<template>
  <AppPage class="bg-[#f5f6fa] h-screen" article-class="" header-class="bg-[#3d8ffd]">
    <template #center>
      <span class="c-white">材料上传</span>
    </template>
    <div class="pb-70">
      <div class="px-15 pt20 pb-20 bg-theme">
        <div class="c-white ">
          <span class="text-18">{{  identityMaterial.applicant_name }}</span>
          <span class="text-15">，请完善一下所需材料！</span>
        </div>
        <div class="c-white text-12">
          为确保您的签证申请顺利通过，请上传真实有效信息！
        </div>
      </div>
      <div class="px-15 pt-15 mb-10">
        <div class="bg-white rounded-6 px-12 pb-12" @click="toArrivalTime">
          <h3 class="pt-10 text-15 pb-6 font-600">期望赴馆城市和日期</h3>
          <div class="flex items-center justify-between p-10 bg-[#f5f6fa] rounded-6">
            <div>
              <span class="text-13 c-[#000]">期望赴馆城市和日期</span>
              <span class="c-[#f00]">*</span>
            </div>
            <div class="flex items-center">
              <span class="text-13 c-[#999]"> {{getCityName(identityMaterial.arrivalCityId)}} {{ identityMaterial.arrivalTime?identityMaterial.arrivalTime+'('+identityMaterial.arrivalCityName+')':'选择' }}</span>
              <Right width="12" class="relative top--1" color="#999" />
            </div>
            <!-- <div v-if="">identityMaterial</div> -->
          </div>
        </div>
      </div>
      <positionData :productMaterial="identityMaterial.material" />
    </div>
    <div class="w-full px-15 box-border fixed bottom-0 left-0  bg-white py-10">
      <div class="c-[#999] text-12 flex items-center mb-5">
        <span class="relative top--3 mr-3">*</span>
        <span>提交后将严格保密，平台会严格保障您的个人信息安全。</span>
      </div>
      <div class="w-full  box-border" @click="submitData">
        <nut-button :block="true" type="info" color="#3d8ffd">提交资料</nut-button>
      </div>
    </div>
  </AppPage>
</template>
<script setup>
import { onMounted, reactive, ref } from 'vue'
import { showToast } from '@nutui/nutui';
import '@nutui/nutui/dist/packages/toast/style';
import { Edit, Plus, Del, Right } from '@nutui/icons-vue';
import { useRoute, useRouter } from 'vue-router'
import datePick from '@/components/datePick/index.vue'
import positionData from '../components/positionData.vue'
import { isEmptyObject } from '@/utils/common/is.js'
import { getCityName } from '@/utils/type/common.js'
import { useVisaStore } from '@/store/modules/visa/index.js'
import { useUserStore } from '@/store/modules/user/index.js'
import { productDetailQuery, getMaterialUploadList, getUploadSign, getOrderDetail, submitApplicantMaterial, updateApplicantMaterial, applicantReverify } from '@/api/visa'
const route = useRoute()
const router = useRouter()
const basicData = reactive({
  travelTime: '2023-12-12',
  contact_name: 'zhonhai',
  contact_number: '18126495952',
  address: ''
})
basicData.travelTime = route.query.travelTime
let identityMaterial = reactive({})
const getIdentityMaterialFun = async () => {
  let identityMaterialStore = !isEmptyObject(useVisaStore().identityMaterial) ? useVisaStore().identityMaterial : (window.localStorage.getItem('identityMaterial') && JSON.parse(window.localStorage.getItem('identityMaterial')))
  for (const key in identityMaterialStore) {
    identityMaterial[key] = identityMaterialStore[key];
  }
  console.log('%c [ identityMaterial ]-70', 'font-size:13px; background:#41fc9e; color:#85ffe2;', identityMaterial)
  getUploadSignFun()
}
const submitData = () => {
  if (+identityMaterial.applicant_status === 1) {
    submitMaterial()
  } else if (+identityMaterial.applicant_status === 2) {
    updateMaterial()
  }
}
const submitMaterial = async () => {
  let _applicant_set = []
  // if (!identityMaterial.arrivalCityName || !identityMaterial.arrivalTime) {
  //   showToast.text('请选择赴馆时间');
  //   return
  // }
  const orderNo = identityMaterial.orderNo
  const { code, data } = await getOrderDetail(orderNo)
  let applicant_set = data.applicant_set
  applicant_set.forEach((item, index) => {
    if (index === identityMaterial.identityIndex) {
      _applicant_set.push({
        identity_id: item.identity_id,
        applicant_id: item.applicant_id,
        applicant_name: item.applicant_name,
        material_zip_url: import.meta.env.VITE_BASE_URL + data.zip_detail_set[index].zip_url,
        city_id: identityMaterial.arrivalCityId,
        date_choice: identityMaterial.date_choice,
        other_date: identityMaterial.other_date,
        preferred_date: identityMaterial.preferred_date,
      })

    } else {
      _applicant_set.push({
        identity_id: item.identity_id,
        applicant_id: item.applicant_id,
        applicant_name: item.applicant_name,
        material_zip_url: import.meta.env.VITE_BASE_URL + data.zip_detail_set[index].zip_url,
        city_id: item.city_id,
        date_choice: item.date_choice,
        other_date: item.other_date,
        preferred_date: item.preferred_date,
      })
    }
  })
  let params = {
    order_id: identityMaterial.order_id,
    applicant_set: _applicant_set
  }
  const { code: _code, data: _data } = await submitApplicantMaterial(params)
  if (+_code === 200) {
    showToast.text('提交资料成功');
    // setTimeout(() => {
    //   router.push({ path: '/orderDetail', query: query })
    // }, 1000);
  }
}
const updateMaterial = async () => {
  let _applicant_set = []
  // if (!identityMaterial.arrivalCityName || !identityMaterial.arrivalTime) {
  //   showToast.text('请选择赴馆时间');
  //   return
  // }
  // {
  //     "order_id":11,
  //     "applicant_id":1,
  //     "material_zip_url":"https://xxx"
  // }
  const orderNo = identityMaterial.orderNo
  const { code, data } = await getOrderDetail(orderNo)
  if (+code !== 200) {
    showToast.text('获取订单资料包失败');
    // setTimeout(() => {
    //   router.push({ path: '/orderDetail', query: query })
    // }, 1000);
  }
  let params = {
    order_id: identityMaterial.order_id,
    applicant_id: identityMaterial.applicant_id,
    material_zip_url: import.meta.env.VITE_BASE_URL + data.zip_detail_set[identityMaterial.identityIndex].zip_url,

  }
  const { code: _code, data: _data } = await updateApplicantMaterial(params)
  if (+_code === 200) {
    // showToast.text('提交资料成功');
    let _params =
    {
      "orderId": identityMaterial.order_id,
      "applicantId": identityMaterial.applicant_id
    }
    const { code: r_code, data: r_data } = await applicantReverify(_params)
    if (+r_code === 200) {
      showToast.text('提交资料成功');
    }
    // setTimeout(() => {
    //   router.push({ path: '/orderDetail', query: query })
    // }, 1000);
  }
}
let query = route.query
const confirmSubmit = async (params) => {

}

const toArrivalTime = () => {
  router.push({ path: 'arrivalTime', query: { countryId: route.query.countryId } })
}
const getUploadSignFun = async () => {
  let uid = useUserStore().uId
  const { code, data } = await getUploadSign(uid)
  if (+code !== 200) return
  useVisaStore().setSign(data)
}
onMounted(async () => {
  getIdentityMaterialFun()
})
</script>
<style scoped>
.card-tabs .n-tabs-nav--bar-type {
  padding-left: 4px;
}
</style>
<style lang="scss" scoped>
.city {
  line-height: 32px;
  border: 1px solid #ddd;
}
.city.active {
  line-height: 32px;
  border: 1px solid #f00;
}
</style>